# -*- coding:utf-8 -*-

from rest_framework import serializers
from basic.models import ElevatorInfo
from manufacturer.models import ElevatorStatus, AuthorityCode


class ElevatorCheckUpSerializer(serializers.ModelSerializer):

    status_id = serializers.SerializerMethodField()
    button_display = serializers.SerializerMethodField()
    installation_place = serializers.SerializerMethodField()

    class Meta:
        model = ElevatorInfo
        fields = ('id', 'register_code', 'num', 'property_company_name', 'installation_place', 'maintain_company_name',
                  'status_id', 'last_checkup_time', 'last_checkup_score', 'button_display', "manufacturer_company_name")

    def get_status_id(self, obj):
        code = obj.manufacturer_company.code if obj.manufacturer_company else None
        if code and obj.factory_no:
            ac = AuthorityCode.objects.filter_active(manufacturer_code_elevator=code, manufacturer_number_elevator=obj.factory_no).first()
            if ac and ac.device_code_elevator:
                es = ElevatorStatus.objects.filter_active(o=ac.device_code_elevator).first()
                if es:
                    return es.id
        return None

    def get_button_display(self, obj):
        if obj.manufacturer_company and obj.manufacturer_company.code == u"1701001":
            return "1"
        return "0"

    def get_installation_place(self, obj):
        return obj.address or obj.installation_place



































